﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml.Serialization;

namespace FileCopy
{
    [Serializable]
    public class f_Directory
    {
        public bool Enabled { get; set; }
        public string DestDirectory { get; set; }
        public f_Directory(string destDir)
        {
            this.DestDirectory = destDir;
        }

        public f_Directory()
            : this(null)
        { }

        public f_Filter[] Filters;

        private TimeSpan startTime = new TimeSpan(0, 0, 0);
        private TimeSpan endTime = new TimeSpan(24, 0, 0);
        [CategoryAttribute("时间设置"), DescriptionAttribute("设置时间范围-开始时间"), DisplayName("起始时间")]
        public string StartTime
        {
            get
            {
                return string.Format("{0:d2}:{1:d2}:{2:d2}", startTime.Hours, startTime.Minutes, startTime.Seconds);
            }
            set
            {
                TimeSpan res;
                if (TimeSpan.TryParse(value, out res))
                {
                    startTime = res;
                }
            }
        }
        [CategoryAttribute("时间设置"), DescriptionAttribute("设置时间范围-结束时间"), DisplayName("结束时间")]
        public string EndTime
        {
            get
            {
                return string.Format("{0:d2}:{1:d2}:{2:d2}", endTime.Hours, endTime.Minutes, endTime.Seconds);
            }
            set
            {
                TimeSpan res;
                if (TimeSpan.TryParse(value, out res))
                {
                    endTime = res;
                }
            }
        }

        public bool IsFileValid(string fileName)
        {
            if (Filters == null || Filters.Length == 0)
                return true;
            if (Filters != null && (this.StartTime == this.EndTime || (DateTime.Now.TimeOfDay >= this.startTime && DateTime.Now.TimeOfDay < this.endTime)))
            {
                foreach (f_Filter filter in Filters)
                {
                    if (filter.CheckValid(fileName))
                        return true;
                }
            }
            return false;
        }

        public string DestFileFullName(string fileName)
        {
            return string.Format("{0}\\{1}", this.DestDirectory, Path.GetFileName(fileName));
        }

        public override string ToString()
        {
            return DestDirectory;
        }
    }
}
